home *** CD-ROM | disk | FTP | other *** search
/ Aminet 50 / Aminet 50 (2002)(GTI - Schatztruhe)[!][Aug 2002].iso / Aminet / text / edit / tecoc-146.lha / makdbf.c < prev    next >
C/C++ Source or Header  |  1991-07-05  |  3KB  |  103 lines

  1. /*****************************************************************************
  2.  
  3.     MakDBf()
  4.  
  5.     This function converts a binary number to it's ASCII equivalent.
  6. The resultant ASCII string is left in the digit buffer DBfBeg.  The second
  7. argument to this function specifies the radix to be used.  If the radix is
  8. decimal,  then a minus sign will be prepended to the number if it is negative.
  9. If the radix is not decimal,  then minus sign are never prepended.
  10.     The recursive algorithm for base 10 conversion comes from "C Traps and
  11. Pitfalls" by Andrew Koenig.  In the "portability problems" section,  the book
  12. discusses common problems converting binary numbers to characters.  One such
  13. problem arises when the maximum negative number is presented to a conversion
  14. algorithm.  Many algorithms don't handle this well,  including the one that
  15. I used to use here.  So I stole his,  which avoids the problem and is also
  16. impervious to the character collating sequence.  I liked using recursion to
  17. order the string so much that I made the base 8 and 16 code use it too.
  18. *****************************************************************************/
  19.  
  20. #include "zport.h"        /* define portability identifiers */
  21. #include "tecoc.h"        /* define general identifiers */
  22. #include "defext.h"        /* define external global variables */
  23.  
  24.  
  25. #if USE_PROTOTYPES
  26. static VVOID DoHex(ULONG n);
  27. static VVOID DoOct(ULONG n);
  28. static VVOID DoNeg(LONG n);
  29. #endif
  30.  
  31. static char digits[] = "0123456789ABCDEF";
  32.  
  33.  
  34. static VVOID DoHex(n)
  35. ULONG n;
  36. {
  37.     if (n != 0) {
  38.         DoHex(n >> 4);
  39.         *DBfPtr++ = digits[(int) n & 0x000f];
  40.     }
  41. }
  42.  
  43.  
  44. static VVOID DoOct(n)
  45. ULONG n;
  46. {
  47.     if (n != 0) {
  48.         DoOct(n >> 3);
  49.         *DBfPtr++ = digits[(int) n & 0x0007];
  50.     }
  51. }
  52.  
  53.  
  54. /*
  55.  *  Put the character representation for the negative number n into DBf
  56.  */
  57. static VVOID DoNeg(n)
  58. LONG n;
  59. {
  60.     LONG quotient;
  61.     DEFAULT remainder;
  62.  
  63.     quotient = n / 10;
  64.     remainder = (int) (n % 10);       /* trailing digit */
  65.     if (remainder > 0) {   /* on some machines, remainder might be positive */
  66.         remainder -= 10;
  67.     quotient++;
  68.     }
  69.     if (n <= -10)
  70.     DoNeg(quotient);
  71.     *DBfPtr++ = digits[-remainder];
  72. }
  73.  
  74.  
  75.  
  76. VVOID MakDBf(Binary, NRadix)    /* make digit buffer (DBfBeg) */
  77. LONG Binary;            /* binary number to be converted */
  78. DEFAULT NRadix;            /* radix to be used: 8, 10 or 16 */
  79. {
  80.         ULONG TmpBin;
  81.  
  82.     DBfPtr = DBfBeg;        /* initialize digit buffer ptr */
  83.     if (Binary == 0) {        /* simple case? */
  84.         *DBfPtr++ = '0';
  85.         return;
  86.     }
  87.     if (NRadix == 10) {
  88.         if (Binary < 0) {
  89.             *DBfPtr++ = '-';
  90.             DoNeg(Binary);
  91.         } else {
  92.             DoNeg(-Binary);
  93.         }
  94.         return;
  95.     }
  96.     TmpBin = Binary;
  97.     if (NRadix == 8) {
  98.         DoOct(TmpBin);
  99.     } else {
  100.         DoHex(TmpBin);
  101.     }
  102. }
  103.